package routines;

// Generated 11.12.2014 23:41:31 by Hibernate Tools 4.3.1

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.*;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;

import static javax.persistence.FetchType.EAGER;

/**
 * NotificationLots generated by hbm2java
 */
@Entity
@Table(name = "notification_lots", indexes = { @Index(columnList = "notification_id", name = "idx_notification_lots_notification_id") })
public class Lot extends Print {

	private Long lotId;
	private BigDecimal maxprice;
	private String currency;
	private String restrictinfo;
	private String addinfo;
	private Integer lotnumber;
	private String lotobjectinfo;
	private BigDecimal applicationGuarantee;
	private BigDecimal contractGuarantee;
	private Notification notification;
	private Map<String,Application> applications;
	
//	private List<PurchaseObject> purchaseobjects = new ArrayList<PurchaseObject>();

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO, generator = "seq_lots_gen")
	@SequenceGenerator(name = "seq_lots_gen", sequenceName = "seq_lots")
	@Column(name = "lot_id", unique = true, nullable = false)
	public Long getLotId() {
		return this.lotId;
	}

	public void setLotId(Long lotId) {
		this.lotId = lotId;
	}

	@OneToMany(mappedBy = "lot")
	@MapKey(name = "journalNumber")
	public Map<String,Application> getApplications() {
		return applications;
	}

	public void setApplications(Map<String,Application> applications) {
		this.applications = applications;
	}

	public String toString() {
		StringBuilder s = new StringBuilder();
		s.append("lotnumber:").append(lotnumber).append(", lotobjectinfo:")
				.append(lotobjectinfo).append(", maxprice:").append(maxprice)
				.append(", contractGuarantee:").append(contractGuarantee);
		// append(", :").append()
		return s.toString();
	}

	public Lot(Integer lotnumber, String lotobjectinfo, BigDecimal maxprice) {
		this.maxprice = maxprice;
		this.lotnumber = lotnumber == null ? 1 : lotnumber;
		this.lotobjectinfo = lotobjectinfo;
	}

	public Lot(Long lotId) {
		this.lotId = lotId;
	}

	public Lot() {

	}

	public Lot(Long lotId, Long notificationId, BigDecimal maxprice,
			String currency, String restrictinfo, String addinfo,
			Integer lotnumber, String lotobjectinfo,
			BigDecimal applicationGuarantee, BigDecimal contractGuarantee) {
		this.lotId = lotId;
		this.maxprice = maxprice;
		this.currency = currency;
		this.restrictinfo = restrictinfo;
		this.addinfo = addinfo;
		this.lotnumber = lotnumber;
		this.lotobjectinfo = lotobjectinfo;
		this.applicationGuarantee = applicationGuarantee;
		this.contractGuarantee = contractGuarantee;
	}


	@Column(name = "maxprice", precision = 21)
	public BigDecimal getMaxprice() {
		return this.maxprice;
	}

	public void setMaxprice(BigDecimal maxprice) {
		this.maxprice = maxprice;
	}
	public void setMaxprice(Double maxprice) {
		if (maxprice!=null)
			this.maxprice = new BigDecimal(maxprice);
	}

	@Column(name = "currency", columnDefinition = "TEXT")
	public String getCurrency() {
		return this.currency;
	}

	public void setCurrency(String currency) {
		this.currency = currency;
	}

	@Column(name = "restrictinfo", columnDefinition = "TEXT")
	public String getRestrictinfo() {
		return this.restrictinfo;
	}

	public void setRestrictinfo(String restrictinfo) {
		this.restrictinfo = restrictinfo;
	}

	@Column(name = "addinfo", columnDefinition = "TEXT")
	public String getAddinfo() {
		return this.addinfo;
	}

	public void setAddinfo(String addinfo) {
		this.addinfo = addinfo;
	}

	@Column(name = "lotnumber")
	public Integer getLotnumber() {
		return this.lotnumber;
	}

	public void setLotnumber(Integer lotnumber) {
		this.lotnumber = lotnumber!=null?lotnumber:1;
	}

	@Column(name = "lotobjectinfo", columnDefinition = "TEXT")
	public String getLotobjectinfo() {
		return this.lotobjectinfo;
	}

	public void setLotobjectinfo(String lotobjectinfo) {
		this.lotobjectinfo = lotobjectinfo;
	}

	@Column(name = "applicationGuarantee", precision = 21)
	public BigDecimal getApplicationGuarantee() {
		return this.applicationGuarantee;
	}

	public void setApplicationGuarantee(BigDecimal applicationGuarantee) {
		this.applicationGuarantee = applicationGuarantee;
	}

	public void setApplicationGuarantee(Double applicationGuarantee) {
		if (applicationGuarantee!=null)
		this.applicationGuarantee = new BigDecimal(applicationGuarantee);
	}

	@Column(name = "contractGuarantee", precision = 21)
	public BigDecimal getContractGuarantee() {
		return this.contractGuarantee;
	}

	public void setContractGuarantee(BigDecimal contractGuarantee) {
		this.contractGuarantee = contractGuarantee;
	}

	public void setContractGuarantee(Double contractGuarantee) {
		if (contractGuarantee!=null)
		this.contractGuarantee = new BigDecimal(contractGuarantee);
	}

	@ManyToOne(fetch = EAGER)
	@JoinColumn(name = "notification_id", nullable = false)
	public Notification getNotification() {
		return notification;
	}

	public void setNotification(Notification notification) {
		this.notification = notification;
	}
	
	public void AddApplication(Application app){
		if (applications == null)
			applications = new HashMap<String, Application>();
		Application a = applications.get(app.getJournalNumber());
		if (a!=null)
			app.setId(a.getId());
		app.setLot(this);
		applications.put(app.getJournalNumber(), app);
	}
	
	public Application GetApplication(String journalNumber){
		if (applications == null) return null;
		return applications.get(journalNumber);
	}
	

//	@OneToMany(fetch = FetchType.LAZY, mappedBy = "lot")
//	@Cascade({ CascadeType.SAVE_UPDATE })
//	@OnDelete(action = OnDeleteAction.CASCADE)
//	public List<PurchaseObject> getPurchaseobjects() {
//		return purchaseobjects;
//	}
//
//	public void setPurchaseobjects(List<PurchaseObject> purchaseobjects) {
//		this.purchaseobjects = purchaseobjects;
//	}
//
//	public void AddPurchaseObject(PurchaseObject obj) {
//		obj.setLot(this);
//		purchaseobjects.add(obj);
//
//	}

}
